package com.oscar.ehome.view.setview.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.oscar.ehome.R;
import com.oscar.ehome.bean.setbean.opinion.CustomConstants;
import com.oscar.ehome.bean.setbean.opinion.ImageItem;
import com.oscar.ehome.provider.gildeimage.GildeUtils;
import com.oscar.ehome.utils.util.ImageDisplayer;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by 柒岇 on 2017/5/31.
 */

public class QuestionAdapter extends BaseAdapter {


    private List<String> mDataList = new ArrayList<String>();
    private Context mContext;

    public QuestionAdapter(Context context, List<String> dataList) {
        this.mContext = context;
        this.mDataList = dataList;
    }

    public void setLists(List<String> dataList) {
        this.mDataList = dataList;
        notifyDataSetChanged();
    }


    public int getCount() {
        // 多返回一个用于展示添加图标
        if (mDataList == null) {
            return 1;
        } else if (mDataList.size() == CustomConstants.MAX_IMAGE_SIZE) {
            return CustomConstants.MAX_IMAGE_SIZE;
        } else {
            return mDataList.size() + 1;
        }
    }

    public Object getItem(int position) {
        if (mDataList != null
                && mDataList.size() == CustomConstants.MAX_IMAGE_SIZE) {
            return mDataList.get(position);
        } else if (mDataList == null || position - 1 < 0
                || position > mDataList.size()) {
            return null;
        } else {
            return mDataList.get(position - 1);
        }
    }

    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("ViewHolder")
    public View getView(int position, View convertView, ViewGroup parent) {
        //所有Item展示不满一页，就不进行ViewHolder重用了，避免了一个拍照以后添加图片按钮被覆盖的奇怪问题
        convertView = View.inflate(mContext, R.layout.item_publish, null);
        ImageView imageIv = (ImageView) convertView
                .findViewById(R.id.item_grid_image);

        if (isShowAddItem(position)) {
            imageIv.setImageResource(R.mipmap.addimg);
        } else {
            String path = mDataList.get(position);
            //使用谷歌官方提供的Glide加载图片
//            GildeUtils.imgLoadPic(mContext, path, imageIv, "nat");

            GildeUtils.loadUrlImages(path, imageIv, mContext);
        }

        return convertView;
    }

    private boolean isShowAddItem(int position) {
        int size = mDataList == null ? 0 : mDataList.size();
        return position == size;
    }
}
